package com.bw.shinelon.yuekaoa;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class Fragment01 extends Fragment{
	private String path = "http://huixinguiyu.cn/Assets/js/data.js";
	private ListView listView;
	private List<Bean.ApkBean> list;
	Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			Gson gson = new Gson();
			Bean bean = gson.fromJson(msg.obj.toString(), Bean.class);
			List<Bean.ApkBean> apk = bean.getApk();
			list.addAll(apk);
			MyBaseAdapter adapter = new MyBaseAdapter();
			listView.setAdapter(adapter);
		}
	};
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		list = new ArrayList<>();
		View view=inflater.inflate(R.layout.fragment01, container, false);
		listView = (ListView) view.findViewById(R.id.lv1);

		boolean nc = NetWorkUtils.isNetworkConnected(getActivity());
		boolean wc = NetWorkUtils.isWifiConnected(getActivity());
		if (nc){
			if (wc){
				getData();
			}else {
				new  AlertDialog.Builder(getActivity())
						.setTitle("提示" )
						.setMessage("是否使用移动数据进行连接？" )
						.setPositiveButton("是", new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								dialog.dismiss();
								getData();
							}
						})
						.setNegativeButton("否" , null)
						.show();
			}
		}else {
			Toast.makeText(getActivity(),"无网络连接！",Toast.LENGTH_SHORT).show();
		}




		return view;
	}
	private void getData(){
		new Thread(){
			@Override
			public void run() {
				new Thread() {
				    @Override
				    public void run() {
				        try {
				            //建立地址对象
				            URL url = new URL(path);
				            //得到对象
				            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
				            //设置请求方式
				            conn.setRequestMethod("GET");
				            //设置响应时间
				            conn.setConnectTimeout(3000);
				            //读取超时时间
				            conn.setReadTimeout(3000);
				            //同步请求
				            if (conn.getResponseCode() == 200) {
				                InputStream is = conn.getInputStream();
				                ByteArrayOutputStream baos = new ByteArrayOutputStream();
				                byte[] buffer = new byte[1024];
				                int len = 0;
				                while ((len = is.read(buffer)) != -1) {
				                    baos.write(buffer, 0, len);
				                }
				                final String s = baos.toString();
								Message message = new Message();
								message.obj = s;
								message.what = 0;
								handler.sendMessage(message);
							}
				        } catch (Exception e) {
				            e.printStackTrace();
				        }
				    }
				}.start();
			}
		}.start();
	}
	class MyBaseAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null){
				convertView = View.inflate(getActivity(),R.layout.item,null);
				holder = new ViewHolder();
				holder.iv = (ImageView) convertView.findViewById(R.id.item_iv);
				holder.tv1 = (TextView) convertView.findViewById(R.id.item_tv1);
				holder.tv2 = (TextView) convertView.findViewById(R.id.item_tv2);
				convertView.setTag(holder);
			}else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.tv1.setText(list.get(position).getName());
			holder.tv2.setText(list.get(position).getCategoryName());
			ImageUtils.setImageView(list.get(position).getIconUrl(),getActivity(),holder.iv);
			return convertView;
		}
		class ViewHolder{
			TextView tv1,tv2;
			ImageView iv;
		}
	}

}